//
//  RequestParams.swift
//  ProjectApp
//
//  Created by jack on 2021/1/8.
//

import Foundation

var dataEnv: DataEnv = .QA(host: ServerDevPath)

var ServerProdPath = "http://www.xxxx.cn"
let ServerDevPath = "http://192.168.0.169:8089"

enum RequestParams: CPRequestParams{
    case Login(account: String, password: String)
    case Register(user: RegisterEntity)
    var target: RequestParamsTarget {
        get {
            switch self {
            case let .Login(account, password):
                return RequestParamsTarget(suffix: "/login", method: .post, params: [
                    "account": account,
                    "password": password
                ])
            case let .Register(user):
                var params: [String: Any]? = nil
                if let data: Data = try? JSONEncoder().encode(user), let dic = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
                    params = dic
                }
                return RequestParamsTarget(suffix: "/register", method: .post, params: params)
            }
        }
    }
    var url: String {
        self.target.url
    }
    
    var method: CPMethod {
        self.target.method
    }
    
    var params: [String : Any]? {
        self.target.params
    }
    
    var headers: [String : String]? {
        getAppHeader()
    }
    
    var cer: Data? {
        nil
    }
}

func getAppHeader() -> [String: String] {
    var header = [String: String]()
    header["app-platform"] = "appPlatform"
    header["app-deviceId"] = "appDeviceId"
    header["app-language"] = "appLanguage"
    header["app-version"] = "appVersion"
    header["app-build"] = "appBuild"
    header["app-systemVersion"] = "systemVersion"
    header["app-device"] = "ios"
    // 登录后所有接口加上token
    header["app-token"] = "token"
    return header;
}

public struct RequestParamsTarget {
    let suffix: String
    let method: CPMethod
    let params: [String: Any]?
    var url: String {
        return getAppServerPath(suffix: suffix)
    }
}

func getAppServerPath(suffix: String) -> String {
    var path = ""
    path = dataEnv.host + suffix
    return path
}

enum DataEnv {
    case QA(host: String)
    case Staging(host: String)
    case Production(host: String)
    case Mock(host: String)
    case Custom(host: String)
    
    var host: String {
        var path = ServerProdPath
        switch self {
        case let .QA(host):
            path = host
        case let .Staging(host):
            path = host
        case let .Production(host):
            path = host
        case let .Mock(host):
            path = host
        case let .Custom(host):
            path = host
        }
        return path
    }
}

